
import { request,gateway } from "@/utils/api.js";


export const useListHook={
  data(){
    return {
      pageNo: 1,
      pageSize: 10,
      total: 0,
	  listUrl:'',
	  dataSouce:[],
	  params:{}
    }
  },
  onLoad() { //页面加载触发的函数，只触发一次
    this.loadData()
  },
  methods:{
	  loadData(init){
		 
		  if(!this.listUrl){
			  uni.showToast({
			  	title:'请设置listurl！',
				icon:'error'
			  })
			  return
		  }
		  if(init){
			  this.pageNo=1
			  this.total=0
			  this.dataSouce=[]
		  }
		  let {pageNo,pageSize,total,params}=this
		  request(this.listUrl,'get',{pageNo,pageSize,...params},true).then(res=>{
		  	if (res.code == 200) {
		  	   this.dataSouce=[...this.dataSouce,...res.result.records]
			   this.total=res.result.total
			   if(pageNo*pageSize<total){
				   this.pageNo+=1
			   }
		  	}
		  })
	  }
  },
  onReachBottom(){
	  let {pageNo,pageSize,total}=this
	  if(pageNo*pageSize<total){
		  this.loadData()
	  }else{
		  uni.showToast({
		  	title:'到底了~'
		  })
	  }
  }
}
